﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PPAP.Model;
using System.Data.OracleClient;

namespace PPAP.DAO
{
    public class AttachmentTypeDAO
    {
        public AttachmentTypeDAO() { }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(AttachmentType model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "ATTACHMENTTYPE");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ATTACHMENTTYPE(");
            strSql.Append("ID,ATTACHMENTTYPE,REMARK,CREATETIME,CREATEDBY,sx,xs)");
            strSql.Append(" values (");
            strSql.Append(":ID,:ATTACHMENTTYPE,:REMARK,:CREATETIME,:CREATEDBY,:sx,:xs)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":ATTACHMENTTYPE", OracleType.VarChar,50),
					new OracleParameter(":REMARK", OracleType.VarChar,50),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":CREATEDBY", OracleType.VarChar,50),
                    new OracleParameter(":sx",OracleType.VarChar,50),
                    new OracleParameter(":xs",OracleType.Number,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.ATTACHMENTTYPE;
            parameters[2].Value = model.REMARK;
            parameters[3].Value = model.CREATETIME;
            parameters[4].Value = model.CREATEDBY;
            parameters[5].Value = model.Sx;
            parameters[6].Value = model.Xs;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters)>0?int.Parse(model.ID.ToString()):0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(AttachmentType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ATTACHMENTTYPE set ");
            strSql.Append("ID=:ID,");
            strSql.Append("ATTACHMENTTYPE=:ATTACHMENTTYPE,");
            strSql.Append("REMARK=:REMARK,");
            strSql.Append("CREATETIME=:CREATETIME,");
            strSql.Append("CREATEDBY=:CREATEDBY,sx=:sx,xs=:xs");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":ATTACHMENTTYPE", OracleType.VarChar,50),
					new OracleParameter(":REMARK", OracleType.VarChar,50),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":CREATEDBY", OracleType.VarChar,50),
                    new OracleParameter(":sx",OracleType.VarChar,50),
                    new OracleParameter(":xs",OracleType.Number,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.ATTACHMENTTYPE;
            parameters[2].Value = model.REMARK;
            parameters[3].Value = model.CREATETIME;
            parameters[4].Value = model.CREATEDBY;
            parameters[5].Value = model.Sx;
            parameters[6].Value = model.Xs;
            

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ATTACHMENTTYPE ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public AttachmentType GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ATTACHMENTTYPE,REMARK,CREATETIME,CREATEDBY,sx,xs from ATTACHMENTTYPE ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            AttachmentType model = new AttachmentType();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.ATTACHMENTTYPE = ds.Tables[0].Rows[0]["ATTACHMENTTYPE"].ToString();
                model.REMARK = ds.Tables[0].Rows[0]["REMARK"].ToString();
                if (ds.Tables[0].Rows[0]["CREATETIME"].ToString() != "")
                {
                    model.CREATETIME = DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
                }
                model.CREATEDBY = ds.Tables[0].Rows[0]["CREATEDBY"].ToString();
                model.Sx = ds.Tables[0].Rows[0]["sx"].ToString();
                model.Xs = int.Parse(ds.Tables[0].Rows[0]["xs"].ToString());
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*,rownum from (select ID,ATTACHMENTTYPE,REMARK,CREATETIME,CREATEDBY,sx,xs ");
            strSql.Append(" FROM ATTACHMENTTYPE where xs=1 order by sx)t ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取需要上传附件的类型
        /// </summary>
        /// <param name="IsLevel"></param>
        /// <returns></returns>
        public DataSet GetIsLevel(string IsLevel)
        {
            string sql = "select p.* from ATTACHMENTTYPE p left join ATTACHMENTTEMPLATE t on p.id=t.attachmenttypeid where t.IsLevel" + IsLevel + "=0  and p.xs=1";
            sql = @"select p.*,y.rid from ATTACHMENTTYPE p left join ATTACHMENTTEMPLATE t on p.id=t.attachmenttypeid
left join(select t.*,rownum rid from(select p.*,t.bdnttemplateavailable,t.uploadedfile from ATTACHMENTTYPE p left join ATTACHMENTTEMPLATE t on p.id=t.attachmenttypeid where    p.xs=1 order by sx) t)y
on p.id=y.id
 where t.IsLevel" + IsLevel + "=0  and p.xs=1";
            return DbHelperOra.Query(sql);
        }

        /// <summary>
        /// 获取有效的附件类型
        /// </summary>
        /// <param name="IsLevel"></param>
        /// <returns></returns>
        public DataSet GetAvailable()
        {
            string sql = "select t.*,rownum from(select p.*,t.bdnttemplateavailable,t.uploadedfile from ATTACHMENTTYPE p left join ATTACHMENTTEMPLATE t on p.id=t.attachmenttypeid where    p.xs=1 order by sx) t";
            return DbHelperOra.Query(sql);
        }

        /// <summary>
        /// 获取需要上传附件的类型
        /// </summary>
        /// <param name="IsLevel"></param>
        /// <returns></returns>
        public DataSet GetIsLevel2(string IsLevel)
        {
            string sql = "select p.* from ATTACHMENTTYPE p left join ATTACHMENTTEMPLATE t on p.id=t.attachmenttypeid where t.IsLevel" + IsLevel + "=1  and p.xs=1";
            return DbHelperOra.Query(sql);
        }
    }
}
